package com.example.wordpress;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Adaptador ListView
 * @author Ignacio Kovacs
 *
 */

public class PostArrayAdapter extends ArrayAdapter<Post> {
	
	private final Context context;
	private final int resource;
	private List<Post> objects;
	
	public PostArrayAdapter(Context context, int resource, List<Post> objects) {
		super(context, resource, objects);
		this.context = context;
		this.resource = resource;
		this.objects = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View item = convertView;
		PostHolder postHolder = null;
		
		if(item == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			item = inflater.inflate(resource, parent, false);
			postHolder = new PostHolder();
			postHolder.label_title = (TextView) item.findViewById(R.id.label_title);
			postHolder.label_modified = (TextView) item.findViewById(R.id.label_modified);
			postHolder.label_status = (TextView) item.findViewById(R.id.label_status);
			postHolder.image_status = (ImageView) item.findViewById(R.id.image_status);
			item.setTag(postHolder);
		}
		else {
			postHolder = (PostHolder) item.getTag();
		}
				
		Post post = objects.get(position);
		postHolder.label_title.setText(post.getTitle());
		postHolder.label_modified.setText(post.getVisualModified());
		postHolder.label_status.setText(post.getStatus());
		
		if(post.getStatus().contentEquals("publish"))
			postHolder.image_status.setImageResource(R.drawable.ic_publish);
		
		else if(post.getStatus().contentEquals("private"))
			postHolder.image_status.setImageResource(R.drawable.ic_private);
		
		else if(post.getStatus().contentEquals("pending"))
			postHolder.image_status.setImageResource(R.drawable.ic_pending);
		
		else if(post.getStatus().contentEquals("draft"))
			postHolder.image_status.setImageResource(R.drawable.ic_draft);
		
		else if(post.getStatus().contentEquals("trash"))
			postHolder.image_status.setImageResource(R.drawable.ic_trash);
		
		return item;
	}
	
	static class PostHolder {
		TextView label_title;
		TextView label_modified;
		TextView label_status;
		ImageView image_status;
	}
}
